package com.yazi.mxz.common.db.dao.impl;


import java.util.HashMap;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.GoodsPromoteDao;
import com.yazi.mxz.common.entity.beans.GoodsPromote;
import com.yazi.mxz.common.entity.beans.GoodsPromoteId;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.enums.GoodsPromoteType;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("goodsPromoteDao")
public class GoodsPromoteDaoImpl extends BasicDao<GoodsPromote> implements GoodsPromoteDao {


	public GoodsPromoteDaoImpl() {
		super(GoodsPromote.class);
		
	}


	@Override
	public Criteria getExample(GoodsPromote gp, String searchWord) {
		Criteria c = super.getExample(gp, searchWord);
		boolean hasGtoreGoodsAlias = false;
		
		// 关联对象条件
		if (null != gp) {
			// ID
			GoodsPromoteId gpId = gp.getId();
			if (null != gpId) {
				if (null != gpId.getStoreGoodsId()) {
					c.add(Restrictions.eq("id.storeGoodsId", gpId.getStoreGoodsId()));
				}
				if (null != gpId.getType()) {
					c.add(Restrictions.eq("id.type", gpId.getType()));
				}
			}			
			// 店铺商品
			StoreGoods storeGoods = gp.getStoreGoods();
			if (null != storeGoods) {
				hasGtoreGoodsAlias = true;
				c.createAlias("storeGoods", "sg");
				// 店铺商品ID
				if (null != storeGoods.getStoreGoodsId()) {
					c.add(Restrictions.eq("sg.storeGoodsId", storeGoods.getStoreGoodsId()));
				} else {
					// 店铺
					Store store = storeGoods.getStore();
					if (null != store) {
						c.createAlias("sg.store", "store");
						// 店铺ID
						if (null != store.getStoreId()) {
							c.add(Restrictions.eq("store.storeId", store.getStoreId()));
						} else {
							// ...店铺其它条件
						}
					}
					// ...店铺商品其它条件
				}			
			}
			// ...其它关联对象条件
		}
		
		// 关键字条件
		if (null != searchWord && !searchWord.isEmpty()) {
			if (!hasGtoreGoodsAlias) {
				c.createAlias("storeGoods", "sg");
			}
			// 关键字可查询商品名称，商品编号
			String likeWord = "%"+ searchWord + "%";
			c.add(Restrictions.or(Restrictions.like("sg.name", likeWord),
						Restrictions.like("sg.barCode", likeWord)));
		}
		
		return c;
	}
	

	@Override
	public int endByTime(GoodsPromoteType type) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("nowTime", DateUtil.getTimeStamp());
		param.put("type", type.ordinal());
		return executeUpdate("endByTimeHql", param);
	}

	@Override
	public int startByTime(GoodsPromoteType type) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("nowTime", DateUtil.getTimeStamp());
		param.put("type", type.ordinal());
		return executeUpdate("startByTimeHql", param);
	}

	@Override
	public GoodsPromote oneNextStart() {
		return unique("oneNextStartHql", null);
	}

	@Override
	public GoodsPromote oneNextEnd() {
		return unique("oneNextEndHql", null);
	}

}
